/**
 * @author Mark Walters
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.components.containers
{
	import flash.display.DisplayObject;
	import com.actionpals.base.BaseSprite;
	
	internal class Container extends BaseSprite
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		/**
		 * Constructor.
		 */		
		public function Container( margin:Number = 5 )
		{
			_margin = margin;
			super();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  margin
		//----------------------------------
		
		/**
		 * @private
		 * Storage for the margin property.
		 */
		protected var _margin:Number;
		
		/**
		 * The number of pixels between elements.
		 */
		public function get margin():Number
		{
			return _margin;
		}
		
		/**
		 * @private
		 */
		public function set margin( value:Number ):void
		{
			_margin = value;
			if( _autoArrange ) arrange();
		}
		
		
		protected var _autoArrange:Boolean = true;
		
		
		public function get autoArrange():Boolean
		{
			return _autoArrange;
		}
		
		public function set autoArrange( value:Boolean ):void
		{
			_autoArrange = value;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods: DisplayObjectContainer
		//
		//--------------------------------------------------------------------------
		
		public override function addChild( child:DisplayObject ):DisplayObject
		{
			var index:int = numChildren;
			var child:DisplayObject = super.addChild( child );
			
			if( _autoArrange ) arrange( index );
			
			return child;
		}
		
		public override function addChildAt( child:DisplayObject, index:int ):DisplayObject
		{
			var child:DisplayObject = super.addChildAt( child, index );
			
			if( _autoArrange ) arrange( index );
			
			return child;
		}
		
		public override function removeChild( child:DisplayObject ):DisplayObject
		{
			var index:int = getChildIndex( child );
			var child:DisplayObject = super.removeChild( child );
			
			if( _autoArrange ) arrange( index );
			
			return child;
		}
		
		public override function removeChildAt( index:int ):DisplayObject
		{
			var child:DisplayObject = super.removeChildAt( index );
			
			if( _autoArrange ) arrange( index );
			
			return child;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function arrange( index:int = 0 ):void
		{
			//Must be implemented in subclasses.
		}
		
	}
}